#include "path.h"

Path::Path()
	: d(new PathPrivate)
{
}

Path::Path(const Path &other)
	: d(other.d)
{
}

void Path::setBegin(Vertex v)
{
	if (d->begin == v)
		return;
	d->begin = v;
	d->end = v;
	d->edges.clear();
}

Vertex Path::begin() const
{
	return d->begin;
}

Vertex Path::end() const
{
	return d->end;
}

bool Path::addEdge(Edge e)
{
	if (e.v() == d->end) {
		d->edges.append(e);
		d->end = e.u();
		return true;
	}
	if (e.u() == d->end) {
		d->edges.append(e);
		d->end = e.v();
		return true;
	}
	return false;
}

QList<Vertex> Path::vertexSequence() const
{
	QList<Vertex> result;
	result.append(d->begin);
	for (int i = 0; i < d->edges.size(); ++i) {
		if (d->edges.at(i).u() == result.last())
			result.append(d->edges.at(i).v());
		else
			result.append(d->edges.at(i).u());
	}
	return result;
}

QList<Edge> Path::edgeSequence() const
{
	return d->edges;
}
